package cn.wolfcode.controller;

import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryItemObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.druid.filter.AutoLoad;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Dictionary;
import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionnaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;
    @RequestMapping("/list")
    @RequiredPermission(name="字典明细显示",expression ="systemDictionaryItem:list")
    public String list(@ModelAttribute("qo") SystemDictionaryItemObject qo, Model model){
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("systemDictionaries",systemDictionaries);
        return "/systemDictionaryItem/list";
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="字典明细删除",expression ="systemDictionaryItem:delete")
    public String delete(Long id){
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="字典明细新增/编辑",expression ="systemDictionaryItem:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        } else {
            systemDictionaryItemService.insert(systemDictionaryItem);
        }
        return new JsonResult();
    }
    @RequestMapping("/getParents")
    @ResponseBody
    public List<SystemDictionaryItem> getParents(Long typeId){
        return systemDictionaryItemService.getParents(typeId);
    }

    @RequestMapping("/selectBySn")
    @ResponseBody
    public List<SystemDictionaryItem> selectBySn(String sn){
        return systemDictionaryItemService.selectBySn(sn);
    }
    @RequestMapping("/selectByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParentId(Long parentId){
        return systemDictionaryItemService.selectByParentId(parentId);
    }
}
